<script setup>
import router from "@/router";
import {useUserStore} from "@/store/modules/user.js";
import {useRoute} from "vue-router";
const route = useRoute();
//简单判断
const validateUsername = (rule, value, callback) => {
  if (value.length === 0) {
    callback(new Error('请输入正确的用户名'))
  } else {
    callback()
  }
}
//简单判断
const validatePassword = (rule, value, callback) => {
  if (value.length === 0) {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const loginForm = reactive({
  username: 'admin',
  password: '123456',
})

const loginRules = reactive({
  username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
})

const label1 = ref('')
const label2 = ref('')
const loginValidate = ref(null)

const focusMap = reactive({
  username: () => label1.value = 'label1',
  password: () => label2.value = 'label2'
})
const userStore = useUserStore()

onBeforeMount(()=>{
  focusMap.username()
  focusMap.password()
})
const blurFun = () => {
  if (!loginForm.username) label1.value = ''
  if (!loginForm.password) label2.value = ''
}

const handleLogin = () => {
  loginValidate.value.validate((valid, error) => {
    if (valid) {
      userStore.login(loginForm).then(()=>{
        const query = route.query

        const redirect = query.redirect|| "/"

        const otherQueryParams = Object.keys(query).reduce(
            (acc, cur) => {
              if (cur !== "redirect") {
                acc[cur] = query[cur];
              }
              return acc;
            },
            {}
        )
        router.push({ path: redirect, query: otherQueryParams });
      })
    } else {
      console.log('error submit!!', error)
      return false
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2>欢迎登录吴大帅系统！</h2>
      <el-form ref="loginValidate" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="form-box">
          <div class="user-box">
            <el-form-item prop="username">
              <el-input
                  v-model="loginForm.username"
                  placeholder="" type="text"
                  @keyup.enter.native="handleLogin"
                  @focus="focusMap.username" @blur="blurFun"
              />
            </el-form-item>
            <!--              <el-input type="text" auto-complete="false" @focus="focusMap.username" @blur="blurFun" v-model="loginForm.username" placeholder=""></el-input>-->
            <label :class="label1" >
              <span class="svg-container"><v-svg-icon icon-class="user" /></span> {{ '\u0020' }}用户名：
            </label>
          </div>
          <div class="user-box">
            <el-form-item prop="password">
              <el-input
                  v-model="loginForm.password"
                  placeholder="" type="password"
                  @keyup.enter.native="handleLogin"
                  @focus="focusMap.password" @blur="blurFun"
              />
            </el-form-item>
            <!--              <el-input type="text" auto-complete="false" @focus="focusMap.password" @blur="blurFun" v-model="loginForm.password" placeholder=""></el-input>-->
            <label :class="label2">
              <span class="svg-container"><v-svg-icon icon-class="password" /></span>{{ '\u0020' }}密码：
            </label>
          </div>
          <a @click="handleLogin">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            Submit
          </a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100%;
  background: linear-gradient(#141e30, #243b55);
  justify-content: center;
  align-items: center;
  .login-box {
    padding: 40px;
    display: flex;
    flex-direction: column;
    background: rgba(0,0,0,.5);
    box-sizing: border-box;
    box-shadow: 0 15px 25px rgba(0,0,0,.6);
    border-radius: 10px;
    h2 {
      margin: 0 0 30px;
      padding: 0;
      color: #fff;
      text-align: center;
    }
    :deep(.el-input__wrapper) {
      margin-bottom: 30px;
      background-color: unset;
      padding: unset;
      box-shadow: unset;
    }
    :deep(.el-input__wrapper:hover) {
      box-shadow: unset;
    }
    :deep(.el-form-item.is-error) {
      .el-input__wrapper {
        box-shadow: unset;
      }
    }
    :deep(.el-form-item__content) {
      .el-form-item__error {
        margin-top: -25px !important;
        color: #049ba2 !important;
      }
    }
    .form-box {
      .user-box {
        position: relative;
        :deep(.el-input__inner) {
          width: 320px;
          padding: 10px 0;
          font-size: 16px;
          color: #fff;
          border: none;
          border-radius: unset;
          border-bottom: 1px solid #fff;
          outline: none;
          background: transparent;
        }
        label {
          position: absolute;
          top:0;
          left: 0;
          padding: 10px 0;
          font-size: 16px;
          color: #fff;
          pointer-events: none;
          transition: .5s;
        }
      }
      a {
        position: relative;
        display: inline-block;
        padding: 10px 20px;
        color: #03e9f4;
        font-size: 16px;
        text-decoration: none;
        text-transform: uppercase;
        overflow: hidden;
        transition: .5s;
        margin-top: 40px;
        letter-spacing: 4px;
        span {
          position: absolute;
          display: block;
        }
        span:nth-child(1) {
          top: 0;
          left: -100%;
          width: 100%;
          height: 2px;
          background: linear-gradient(90deg, transparent, #03e9f4);
          animation: btn-anim1 1s linear infinite;
        }

        span:nth-child(2) {
          top: -100%;
          right: 0;
          width: 2px;
          height: 100%;
          background: linear-gradient(180deg, transparent, #03e9f4);
          animation: btn-anim2 1s linear infinite;
          animation-delay: .25s
        }

        span:nth-child(3) {
          bottom: 0;
          right: -100%;
          width: 100%;
          height: 2px;
          background: linear-gradient(270deg, transparent, #03e9f4);
          animation: btn-anim3 1s linear infinite;
          animation-delay: .5s
        }

        span:nth-child(4) {
          bottom: -100%;
          left: 0;
          width: 2px;
          height: 100%;
          background: linear-gradient(360deg, transparent, #03e9f4);
          animation: btn-anim4 1s linear infinite;
          animation-delay: .75s
        }

      }
      a:hover {
        background: #03e9f4;
        color: #fff;
        border-radius: 5px;
        box-shadow: 0 0 5px #03e9f4,
        0 0 25px #03e9f4,
        0 0 50px #03e9f4,
        0 0 100px #03e9f4;
      }
    }
  }
}
.label1 {
  top: -20px !important;
  left: 0 !important;
  color: #03e9f4 !important;
  font-size: 12px !important;
}
.label2 {
  top: -20px !important;
  left: 0 !important;
  color: #03e9f4 !important;
  font-size: 12px !important;
}
@keyframes btn-anim1 {
  0% {
    left: -100%;
  }
  50%,100% {
    left: 100%;
  }
}


@keyframes btn-anim2 {
  0% {
    top: -100%;
  }
  50%,100% {
    top: 100%;
  }
}


@keyframes btn-anim3 {
  0% {
    right: -100%;
  }
  50%,100% {
    right: 100%;
  }
}


@keyframes btn-anim4 {
  0% {
    bottom: -100%;
  }
  50%,100% {
    bottom: 100%;
  }
}

</style>
